<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Jadwal_pmb extends CI_Controller {
	
	function __construct(){
		parent::__construct();
		$this->load->model('mdl_jadwal_pmb');
	}
	
	function index(){
		$this->load->view('jadwal_pmb');
	}
	
	function grid(){
		$data = $this->mdl_jadwal_pmb->getdata();
		echo $this->mdl_jadwal_pmb->togrid($data['row_data'], $data['row_count']);
	}
	
	function grid_detail($PMBPeriodID){
		echo $this->mdl_jadwal_pmb->getdata_detail($PMBPeriodID);
	}
	
	function add(){		
		$data['kode'] = '';
		$data['PMBPeriodID'] = '';
		$data['kd_pt'] = '';
		$data['Nama'] = '';
		$data['TglMulai'] = date('d/m/Y');
		$data['TglSelesai'] = date('d/m/Y');
		$data['UjianMulai'] = date('d/m/Y');
		$data['UjianSelesai'] = date('d/m/Y');
		$data['BayarMulai'] = date('d/m/Y');
		$data['BayarSelesai'] = date('d/m/Y');
		$data['TelitiBayarProdi1'] = '';
		$data['TelitiBayarProdi2'] = '';
		$data['TelitiBayarProdi3'] = '';
		$data['TelitiBayarProdi4'] = '';
		$data['TelitiBayarProdi5'] = '';
		$data['TelitiBayarProdi6'] = '';
		$data['TelitiBayarProdi7'] = '';
		$data['TelitiBayarProdi8'] = '';
		$data['TelitiBayarProdi9'] = '';
		$data['TelitiBayarProdi10'] = '';
		$data['status'] = 'A';
		$this->load->view('jadwal_pmb_form', $data);
	}
	
	function edit($kode){
		$r = $this->mdl_jadwal_pmb->getdataedit($kode);
		
		$data['kode'] = $kode;
		$data['PMBPeriodID'] = $r->row()->PMBPeriodID;
		$data['kd_pt'] = $r->row()->kd_pt;
		$data['Nama'] = $r->row()->Nama;
		$data['TglMulai'] = FormatDateFromMysql($r->row()->TglMulai);
		$data['TglSelesai'] = FormatDateFromMysql($r->row()->TglSelesai);
		$data['UjianMulai'] = FormatDateFromMysql($r->row()->UjianMulai);
		$data['UjianSelesai'] = FormatDateFromMysql($r->row()->UjianSelesai);
		$data['BayarMulai'] = FormatDateFromMysql($r->row()->BayarMulai);
		$data['BayarSelesai'] = FormatDateFromMysql($r->row()->BayarSelesai);
		
		$TelitiBayarProdi = $r->row()->TelitiBayarProdi;
		$data['TelitiBayarProdi1'] = strpos($TelitiBayarProdi, 'Agribisnis')!==false?'Agribisnis':'';
		$data['TelitiBayarProdi2'] = strpos($TelitiBayarProdi, 'Agro Eko Teknologi')!==false?'Agro Eko Teknologi':'';
		$data['TelitiBayarProdi3'] = strpos($TelitiBayarProdi, 'Akutansi')!==false?'Akutansi':'';
		$data['TelitiBayarProdi4'] = strpos($TelitiBayarProdi, 'Biologi')!==false?'Biologi':'';
		$data['TelitiBayarProdi5'] = strpos($TelitiBayarProdi, 'Ekonomi Pembangunan')!==false?'Ekonomi Pembangunan':'';
		$data['TelitiBayarProdi6'] = strpos($TelitiBayarProdi, 'Geologi')!==false?'Geologi':'';
		$data['TelitiBayarProdi7'] = strpos($TelitiBayarProdi, 'Kehutanan')!==false?'Kehutanan':'';
		$data['TelitiBayarProdi8'] = strpos($TelitiBayarProdi, 'Manejmen')!==false?'Manejmen':'';
		$data['TelitiBayarProdi9'] = strpos($TelitiBayarProdi, 'Manejemen Sumberdaya Pesisir')!==false?'Manejemen Sumberdaya Pesisir':'';
		$data['TelitiBayarProdi10'] = strpos($TelitiBayarProdi, 'Sistem Informasi')!==false?'Sistem Informasi':'';
		
		$data['status'] = $r->row()->status;
		
		$this->load->view('jadwal_pmb_form', $data);
	}
	
	function save($aksi){
		# init
		$status = "";
		$result = false;
		$data['pesan_error'] = '';
		
		# get post data
		foreach($_POST as $key => $value){
			$data[$key] = $value;
		}
		$data['Logo'] = '';
		
		# rules validasi form
		$this->form_validation->set_rules("PMBPeriodID", 'Kode Jadwal PMB', 'trim|required|xss_clean');
		$this->form_validation->set_rules("kd_pt", 'Universitas', 'trim|required|xss_clean');
		$this->form_validation->set_rules("Nama", 'Nama Jadwal PMB', 'trim|required|xss_clean');
		$this->form_validation->set_rules("TglMulai", 'Tanggal Mulai', 'trim|required|xss_clean');
		$this->form_validation->set_rules("TglSelesai", 'Tanggal Selesai', 'trim|required|Email|xss_clean');
		
		# message rules
		$this->form_validation->set_message('required', 'Field %s harus diisi.');

		$data['pesan_error'] = '';
		if ($this->form_validation->run() == FALSE){
			//$data["pesan_error"] .= trim(form_error('PMBPeriodID',' ',' '))==''?'':form_error('PMBPeriodID',' ',' ').'<br>';
			$data["pesan_error"] .= trim(validation_errors(' ',' '))==''?'':validation_errors(' ',' ');
		}else{			
			if($aksi=="add"){ // add
				$result = !$this->mdl_jadwal_pmb->isExistKode($data['PMBPeriodID']);
				if ($result){
					$result = $this->mdl_jadwal_pmb->InsertOnDb($data);
				}else{
					$data['pesan_error'] .= 'Sudah terdapat kode dalam database!';
				}
			}else { // edit
				if($data['kode'] == $data['PMBPeriodID']){ // jika kode tidak diubah
					$result=$this->mdl_jadwal_pmb->UpdateOnDb($data);
				}else{
					$result = !$this->mdl_jadwal_pmb->isExistKode($data['PMBPeriodID']);
					if ($result){
						$result=$this->mdl_jadwal_pmb->UpdateOnDb($data);
					}else{
						$data['pesan_error'] .= 'Sudah terdapat kode dalam database!';
					}
				}
			}
		}
		
		if($result){
			echo json_encode(array('success'=>true));
		}else{
			echo json_encode(array('msg'=>$data['pesan_error']));
		}
	}
	
	function delete($kode){
		$result = $this->mdl_jadwal_pmb->DeleteOnDb($kode);
		if ($result){
			echo json_encode(array('success'=>true));
		} else {
			echo json_encode(array('msg'=>'Data gagal dihapus'));
		}
	}
	
}